package com.bbva.tasks;


import com.microstrategy.utils.log.Level;
import com.microstrategy.web.app.tasks.BeanTaskRequestContext;
import com.microstrategy.web.app.tasks.ReportExecutionTask;
import com.microstrategy.web.beans.ReportBean;
import com.microstrategy.web.beans.TransformInstance;
import com.microstrategy.web.beans.WebBeanException;
import com.microstrategy.web.beans.WebComponent;
import com.microstrategy.web.beans.WebException;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.web.objects.WebReportInstance;
import com.microstrategy.web.objects.WebReportSource;
import com.microstrategy.web.tasks.TaskException;
import com.microstrategy.web.tasks.TaskRequestMalformedException;
import com.microstrategy.web.transform.KeyAlreadyExistsException;
import com.microstrategy.web.transform.StyleCatalog;
import com.microstrategy.web.transform.Transform;

public class CustAsyncReportExecTask extends ReportExecutionTask {

	private String sVersion="DHLM 1.9.multi 2013-01-28";

	public CustAsyncReportExecTask() {
		super();
		String FUNCTION_NAME = "CustomReportExecutionTask";
		Log.logger.logp(Level.INFO, CLASS_NAME, FUNCTION_NAME ,"["+sVersion+ "]Start Custom Report Execution");
	}
	
	protected void handleEvent(BeanTaskRequestContext beantaskrequestcontext)
	throws TaskException
    {
		String FUNCTION_NAME ="handleEvent";
		System.out.println("handleEvent");
		Log.logger.logp(Level.INFO, CLASS_NAME, FUNCTION_NAME  , " Custom Report Execution");
		 try
	     {
			 WebComponent webComponent = beantaskrequestcontext.getWebComponent();
	         webComponent.setTargetBean(beantaskrequestcontext.getWebBean());
	         StyleCatalog styleCatalog = beantaskrequestcontext.getStyleCatalog();
	         Transform transform=styleCatalog.getTransformByStyle("ResultSetStatusStyle");
	         setRbAsync(beantaskrequestcontext,transform);
	         //beantaskrequestcontext.getWebBean().transform();
	         webComponent.handleRequest(beantaskrequestcontext.getRequestKeys());
	      }
	     catch(Exception exception)
	     {
	    	 exception.printStackTrace();
	    	 throw new TaskRequestMalformedException((new StringBuilder()).append("The task '").append(getID()).append("' could not be completed due to an event handling error: ").append(exception.getMessage()).toString());
	     }
    }
	
	private void setRbAsync(BeanTaskRequestContext btrContext, Transform transform) throws WebBeanException, WebException, TaskException, WebObjectsException, KeyAlreadyExistsException
	{
		System.out.println("entro al setAsync");
		String reportId = btrContext.getRequestKeys().getValue("reportId");
		ReportBean rb = (ReportBean)btrContext.getWebBean();
		WebIServerSession isess = btrContext.getWebIServerSession("sessionState", null);
		WebObjectsFactory woFact = isess.getFactory();
		WebReportSource reptSrc = woFact.getReportSource();
		WebReportInstance rptInst = reptSrc.getNewInstance(reportId);
		rptInst.setMaxWait(0);
		rptInst.setAsync(true);
		TransformInstance ti1 = rb.addTransform(transform);
        ti1.setKey("ResultSetStatusStyle");
		rb.setReportInstance(rptInst);
		rb.setResultFlags(33554432);
		rb.setExecutionFlags(2176);
		rb.setMaxWait(0);
		rb.collectMinimalData();
		//rptInst.getResults();
		System.out.println(rptInst.getMessageID());
		btrContext.setWebBean(rb);
		System.out.println("SetAsync");
	}

	 private String CLASS_NAME = "com.bbva.tasks.CustomReportExecutionTask";
	 
}